package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.ProductContentMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.ProductQuery;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductContentMapper productContentMapper;

    @Override
    public void saveOrUpdate(Product product) {
        System.out.println(product.getId());
        if(product.getId()!=null){
            productMapper.updateByPrimaryKey(product);
            productContentMapper.updateByPrimaryKey(product.getProductContent());
        }else{
            productMapper.insert(product);
            //文本对象关联主键id
            ProductContent content = product.getProductContent();
            content.setId(product.getId());
            productContentMapper.insert(content);
        }
    }

    @Override
    public PageInfo query(ProductQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Product> list = productMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateState(Product product) {

    }

    @Override
    public List<Product> listAll(int state) {
        return productMapper.selectAll(state);
    }

    @Override
    public Product get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

}
